package com.kuang.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Author: Zhang Dongwei
 * Date: 2022/3/13 14:46
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        new Thread(new Runnable()).start();
//        new Thread(new FutureTask<V>()).start();
//        new Thread(new FutureTask<V>(Callable)).start();

        new Thread().start();//怎么启动callable
        MyThread thread = new MyThread();
        FutureTask futureTask = new FutureTask(thread);  // 适配类

        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start(); // 结果会被缓存，效率高
        Integer o = (Integer) futureTask.get(); //获取Callable的返回结果, 这个get方法可能会产生阻塞！把他放在最后一行，或者使用异步通信来处理
        System.out.println(o);
    }
}
class MyThread implements Callable<Integer> { // 泛型的参数等于方法的返回值（Integer）

    @Override
    public Integer call(){
        System.out.println("call()");
        // 耗时操作
        return 1024;
    }
}